﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Windows.Forms;
namespace VersionManage.Common
{
    public class ErrorHander
    {
        public static string ErrorLogPath = AppDomain.CurrentDomain.BaseDirectory + @"Error.txt";
        public static string RequestErrorLogPath = AppDomain.CurrentDomain.BaseDirectory + @"RequestError.txt";
        public static string RecoredLogPath = AppDomain.CurrentDomain.BaseDirectory + @"Recored.txt";
        public static System.Collections.Concurrent.ConcurrentQueue<ErrorModel> queueInfo = new System.Collections.Concurrent.ConcurrentQueue<ErrorModel>();
        private static bool flag = false;

        /// <summary>
        /// 写非正常错误
        /// </summary>
        /// <param name="info"></param>
        public static void WriteError(string info)
        {
            ErrorModel model = new ErrorModel();
            model.ErrorInfo = info;
            model.Type = 1;
            queueInfo.Enqueue(model);
        }
        /// <summary>
        /// 记录请求错误
        /// </summary>
        /// <param name="info"></param>
        public static void WriteRequestError(string info)
        {
            ErrorModel model = new ErrorModel();
            model.ErrorInfo = info;
            model.Type = 2;
            queueInfo.Enqueue(model);
        }
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="info"></param>
        public static void WriteRecord(string info)
        {
            ErrorModel model = new ErrorModel();
            model.ErrorInfo = info;
            model.Type = 3;
            queueInfo.Enqueue(model);
        }

        /// <summary>
        /// 开始写错误日志
        /// </summary>
        private static void Write()
        {
            while (true)
            {
                if (queueInfo.Count > 0)
                {
                    ErrorModel model = null;
                    if (queueInfo.TryDequeue(out model))
                    {
                        if (model.Type == 1)
                        {
                            FileInfo fileinfo = new FileInfo(ErrorLogPath);
                            long length = 0;
                            if (fileinfo.Exists)
                            {
                                length = fileinfo.Length;
                                if (length > 1024 * 1024)
                                {
                                    ErrorLogPath = Application.ExecutablePath + @"Error" + DateTime.Now.ToString("HHmmss") + ".txt";
                                }
                            }

                            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(ErrorLogPath, true))
                            {
                                sw.WriteLine(model.ErrorInfo);
                            }
                        }
                        else if (model.Type == 2)
                        {
                            FileInfo fileinfo = new FileInfo(RequestErrorLogPath);
                            long length = 0;
                            if (fileinfo.Exists)
                            {
                                length = fileinfo.Length;
                                if (length > 1024 * 1024)
                                {
                                    RequestErrorLogPath = Application.ExecutablePath + @"RequestError" + DateTime.Now.ToString("HHmmss") + ".txt";
                                }
                            }
                            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(RequestErrorLogPath, true))
                            {
                                sw.WriteLine(model.ErrorInfo);
                            }
                        }
                        else if (model.Type == 3)
                        {
                            FileInfo fileinfo = new FileInfo(RecoredLogPath);
                            long length = 0;
                            if (fileinfo.Exists)
                            {
                                length = fileinfo.Length;
                                if (length > 1024 * 1024)
                                {
                                    RecoredLogPath = Application.ExecutablePath + @"Recored" + DateTime.Now.ToString("HHmmss") + ".txt";
                                }
                            }
                            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(RecoredLogPath, true))
                            {
                                sw.WriteLine(model.ErrorInfo);
                            }
                        }
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(100);
                }
            }
        }

        /// <summary>
        /// 开始写错误日志
        /// </summary>
        public static void WriteStatic()
        {
            if (!flag)
            {
                flag = true;
                System.Threading.Thread thread = new System.Threading.Thread(new ThreadStart(Write));
                thread.IsBackground = true;
                thread.Start();
            }
        }
    }


    public class ErrorModel
    {
        public string ErrorInfo = string.Empty;
        public int Type = 0;
    }
}
